package com.boco.blch.dispatcher.util;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class AnalyzeXmlInfo {

	private static String ipcomplianceCount;
	private static String ipuncomplianceCount;
	private static String iptotalCount;
	private static String ipcomplianceRate;
	private static String cmpName;
	private static String cmpComplianceRate;
	private static String cmpComplianceCount;
	private static String cmpUncomplianceCount;
	private static String cmpTotalCount;
	private static String deviceCountString;
	private static String complianceRateString;
	private static String totalCountString;
	private static String complianceCountString;
	private static String uncomlianceCountString;
	private Map<String, Object> infoMap = new HashMap<String,Object>();
	//private List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
	//private List<Map<String, Object>> componentList = new ArrayList<Map<String, Object>>();
	private List<Map<String, Object>> componentMapList =new ArrayList<Map<String, Object>>();
	private  List<Map<String, Object>> resultMapList = new ArrayList<Map<String, Object>>();
	private List<Map<String, Object>> resultSetMapList = new ArrayList<Map<String, Object>>();
	public Map<String, Object> XmlInfo(String url, Map<String, String> params) {
		String ipString = null;
		SAXReader reader = new SAXReader();
		String xmlInfoString = HttpXmlClient.sendPost(url, params);
		try {
			// 读取Xml数据并获得根节点
			reader.setEncoding("UTF8");
			Document document = DocumentHelper.parseText(xmlInfoString);
			Element root = document.getRootElement();
			// 数据检查的整体情况放入allInfoMap
			Map<String, Object> allInfoMap = new HashMap<String, Object>();
			deviceCountString = root.element("deviceCount").getText();
			complianceRateString = root.element("complianceRate").getText();
			totalCountString = root.element("totalCount").getText();
			complianceCountString = root.element("complianceCount").getText();
			uncomlianceCountString = root.element("uncomlianceCount").getText();
			allInfoMap.put("deviceCountString", deviceCountString);
			allInfoMap.put("complianceRateString", complianceRateString);
			allInfoMap.put("totalCountString", totalCountString);
			allInfoMap.put("complianceCountString", complianceCountString);
			allInfoMap.put("uncomlianceCountString", uncomlianceCountString);
			infoMap.put("allInfoMap",allInfoMap);
			// results
			Element resultsElement = root.element("results");
			// 得到所有的result并存放在集合resultList中
			List<Element> resultElementList = resultsElement.elements();

			// 遍历resultList获取所有result下的信息
			for (int i = 0; i < resultElementList.size(); i++) {
				
				Element result = resultElementList.get(i);
				// 存放result下的信息的Map
				Map<String, Object> resultMap = new HashMap<String, Object>();
				// 获取result下的所有元素，包括（<ip>,<component>）
				@SuppressWarnings("unchecked")
				List<Element> componentElementList = result.elements();
				// 获取 result下的ip的信息，存放于一个ipInfo中
				Element ip = result.element("ip");
				ipString = ip.getText();
				Map<String, Object> ipInfo = new HashMap<String, Object>();
				ipcomplianceRate = ip.attribute("complianceRate").getValue();
				ipcomplianceCount = ip.attribute("complianceCount").getValue();
				ipuncomplianceCount = ip.attribute("uncomplianceCount").getValue();
				iptotalCount = ip.attribute("totalCount").getValue();
				ipInfo.put("ipcomplianceRate", ipcomplianceRate.replace("\"", "&quot;"));
				ipInfo.put("ipcomplianceCount", ipcomplianceCount.replace("\"", "&quot;"));
				ipInfo.put("ipuncomplianceCount", ipuncomplianceCount.replace("\"", "&quot;"));
				ipInfo.put("iptotalCount", iptotalCount.replace("\"", "&quot;"));
				ipInfo.put("ipString", ipString.replace("\"", "&quot;"));
				resultMap.put("ipInfo", ipInfo);
				// 遍历除了ip以外result下的所有component元素
				for (int j = 1; j < componentElementList.size(); j++) {
					Element component = componentElementList.get(j);
					// 获取
					// component下的component的信息，存放于一个Componetmap中内容包括component自带属性和下面的resultList
					Map<String, Object> Componetmap = new HashMap<String, Object>();
					// 将component自带属性查出放入componentmap
					cmpName = component.attribute("name").getValue();
					cmpComplianceRate = component.attribute("complianceRate").getValue();
					cmpComplianceCount = component.attribute("complianceCount").getValue();
					cmpUncomplianceCount = component.attribute("uncompolianceCount").getValue();
					cmpTotalCount = component.attribute("totalCount").getValue();
					Componetmap.put("cmpName", cmpName.replace("\"", "&quot;"));
					Componetmap.put("cmpComplianceRate", cmpComplianceRate.replace("\"", "&quot;"));
					Componetmap.put("cmpComplianceCount", cmpComplianceCount.replace("\"", "&quot;"));
					Componetmap.put("cmpUncomplianceCount", cmpUncomplianceCount.replace("\"", "&quot;"));
					Componetmap.put("cmpTotalCount", cmpTotalCount.replace("\"", "&quot;"));
					// results-result-component-detail
					Element detailElement = component.element("detail");
					// 获取results-result-component-detail-resultSet
					@SuppressWarnings("unchecked")
					List<Element> resultsetElementList = detailElement.elements();
					
					for (int h = 0; h < resultsetElementList.size(); h++) {
						Map<String, Object> resulteSetmMap = new HashMap<String, Object>();
						Element resultSetElement = resultsetElementList.get(h);
						String Serialnumber = resultSetElement.element("serialnumber").getText();
						String compliancenumber = resultSetElement.element("compliancenumber").getText();
						String compliance = resultSetElement.element("compliance").getText();
						String name = resultSetElement.element("name").getText();
						String des = resultSetElement.element("des").getText();
						String standard = resultSetElement.element("standard").getText();
						String checkStep = resultSetElement.element("checkStep").getText();
						// checkStep.toString().replace("\"", "&quot;");
						resulteSetmMap.put("Serialnumber", Serialnumber.replace("\"", "&quot;"));
						resulteSetmMap.put("compliancenumber", compliancenumber.replace("\"", "&quot;"));
						resulteSetmMap.put("compliance", compliance.replace("\"", "&quot;"));
						resulteSetmMap.put("name", name.replace("\"", "&quot;"));
						resulteSetmMap.put("des", des.replace("\"", "&quot;"));
						resulteSetmMap.put("standard", standard.replace("\"", "&quot;"));
						resulteSetmMap.put("checkStep", checkStep.replace("\"", "&quot;"));
						resulteSetmMap.put("DeviceIp", ipString);
						Float a = (float) Integer.parseInt(cmpUncomplianceCount);
						Float b = (float) Integer.parseInt(cmpTotalCount);
						Float xFloat = (float) (a / b) * 100;
						DecimalFormat decimalFormat = new DecimalFormat(".0");
						String xString = decimalFormat.format(xFloat);
						resulteSetmMap.put("influencePercenString", xString + "%");
						resulteSetmMap.put("appearenceTime", cmpUncomplianceCount);
						resulteSetmMap.put("totalDevice", cmpTotalCount);						
						resultSetMapList.add(resulteSetmMap);										
					}
					Componetmap.put("resultSetMapList", resultSetMapList);
					componentMapList.add(Componetmap);
					
				}
				//componentMapList.put("componentList",componentList);
				//resultList.add();
				resultMap.put("componentMapList", componentMapList);
				resultMapList.add(resultMap);
			}
			infoMap.put("resultMapList",resultMapList);

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return infoMap;

	}

}
